local assets =
{
    Asset("ANIM", "anim/doro_orange_hat.zip")
}
local function ruinshat_fxanim(inst)
    inst._fx.AnimState:PlayAnimation("hit")
    inst._fx.AnimState:PushAnimation("idle_loop")
end

local function ruinshat_oncooldown(inst)
    inst._task = nil
end

local function ruinshat_unproc(inst)
    if inst:HasTag("forcefield") then
        inst:RemoveTag("forcefield")
        if inst._fx ~= nil then
            inst._fx:kill_fx()
            inst._fx = nil
        end
        inst:RemoveEventCallback("armordamaged", ruinshat_fxanim)

        inst.components.armor:SetAbsorption(0.85)
        inst.components.armor.ontakedamage = nil

        --[[ if inst._task ~= nil then
            inst._task:Cancel()
        end
        inst._task = inst:DoTaskInTime(TUNING.ARMOR_RUINSHAT_COOLDOWN, ruinshat_oncooldown) ]]
        ruinshat_oncooldown(inst)
    end
end

local function ruinshat_proc(inst, owner)
    inst:AddTag("forcefield")
    if inst._fx ~= nil then
        inst._fx:kill_fx()
    end
    inst._fx = SpawnPrefab("forcefieldfx")
    inst._fx.entity:SetParent(owner.entity)
    inst._fx.Transform:SetPosition(0, 0.2, 0)
    inst:ListenForEvent("armordamaged", ruinshat_fxanim)

    inst.components.armor:SetAbsorption(TUNING.FULL_ABSORPTION)
    inst.components.armor.ontakedamage = function(inst, damage_amount)
        if owner ~= nil and owner.components.sanity ~= nil then
            owner.components.sanity:DoDelta(-damage_amount * TUNING.ARMOR_RUINSHAT_DMG_AS_SANITY, false)
        end
    end

    if inst._task ~= nil then
        inst._task:Cancel()
    end
    inst._task = inst:DoTaskInTime(TUNING.ARMOR_RUINSHAT_DURATION, ruinshat_unproc)
end

local function tryproc(inst, owner, data)
    if inst._task == nil and
        not data.redirected and
        math.random() < (1 / 3) then
        ruinshat_proc(inst, owner)
    end
end
local function onequip(inst, owner)
    owner.AnimState:OverrideSymbol("swap_hat", "doro_orange_hat", "swap_hat")
    owner.AnimState:Show("HAT")
    owner.AnimState:Hide("HAIR_HAT")
    owner.AnimState:Show("HAIR_NOHAT")
    owner.AnimState:Show("HAIR")

    owner.AnimState:Show("HEAD")
    owner.AnimState:Hide("HEAD_HAT")
    owner.AnimState:Hide("HEAD_HAT_NOHELM")
    owner.AnimState:Hide("HEAD_HAT_HELM")

    owner.doro_ignoreretarget = true

    if owner.components.builder ~= nil then
        owner.components.builder.ingredientmod = TUNING.GREENAMULET_INGREDIENTMOD
    end

    owner:AddTag("fireimmune")
    if owner.components.temperature then
        owner.components.temperature:SetTemp(TUNING.STARTING_TEMP)
    end
    if not owner.components.rainimmunity then
        owner:AddComponent("rainimmunity")
    end
    owner.components.rainimmunity:AddSource(inst)
    inst.onattach(owner)

    if not inst.doro_hat_aura or not inst.doro_hat_aura:IsValid() then
        inst.doro_hat_aura = SpawnPrefab("doro_hat_aura")
    end
    inst.doro_hat_aura.entity:SetParent(owner.entity)
    if inst._light == nil then
        inst._light = SpawnPrefab("minerhatlight")
        inst._light.Light:SetRadius(8)
        inst._light.entity:SetParent(owner.entity)
    end

    if inst.components.container ~= nil then
        inst.components.container:Open(owner)
    end
end

local function onunequip(inst, owner)
    owner.AnimState:ClearOverrideSymbol("headbase_hat")
    owner.AnimState:ClearOverrideSymbol("swap_hat")
    owner.AnimState:Hide("HAT")
    owner.AnimState:Hide("HAIR_HAT")
    owner.AnimState:Show("HAIR_NOHAT")
    owner.AnimState:Show("HAIR")

    if owner:HasTag("player") then
        owner.AnimState:Show("HEAD")
        owner.AnimState:Hide("HEAD_HAT")
        owner.AnimState:Hide("HEAD_HAT_NOHELM")
        owner.AnimState:Hide("HEAD_HAT_HELM")
    end

    owner.doro_ignoreretarget = nil
    if owner.components.builder ~= nil then
        owner.components.builder.ingredientmod = 1
    end

    owner:RemoveTag("fireimmune")

    if owner.components.temperature then
        owner.components.temperature:SetTemp()
    end

    if owner.components.rainimmunity then
        owner.components.rainimmunity:RemoveSource(inst)
    end

    inst.ondetach()
    if inst.doro_hat_aura and inst.doro_hat_aura:IsValid() then
        inst.doro_hat_aura:Remove()
    end

    if inst._light ~= nil then
        inst._light:Remove()
        inst._light = nil
    end

    if inst.components.container ~= nil then
        inst.components.container:Close(owner)
    end
end
local function onequiptomodel(inst, owner, from_ground)
    if inst.components.container ~= nil then
        inst.components.container:Close(owner)
    end
end
local function ruins_onremove(inst)
    if inst._fx ~= nil then
        inst._fx:kill_fx()
        inst._fx = nil
    end
end
local function fn()
    local inst = CreateEntity()

    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank("doro_orange_hat")
    inst.AnimState:SetBuild("doro_orange_hat")
    inst.AnimState:PlayAnimation("anim")

    MakeInventoryFloatable(inst, "med")

    inst.entity:SetPristine()

    inst:AddTag("waterproofer")
    inst:AddTag("hide_percentage")
    inst:AddTag("goggles")
    inst:AddTag("doro_hat")
    inst:AddTag("nosteal")

    if not TheWorld.ismastersim then
        return inst
    end

    inst:AddComponent("inventoryitem")
    inst.components.inventoryitem.keepondrown = true
    inst.components.inventoryitem.keepondeath = true

    MakeHauntableLaunch(inst)

    inst:AddComponent("inspectable")

    inst:AddComponent("armor")
    inst.components.armor:InitIndestructible(0.85)

    inst:AddComponent("planardefense")


    inst:AddComponent("equippable")
    inst.components.equippable:SetOnEquip(onequip)
    inst.components.equippable:SetOnUnequip(onunequip)
    inst.components.equippable:SetOnEquipToModel(onequiptomodel)
    inst.components.equippable.restrictedtag = "doro"
    inst.components.equippable.equipslot = EQUIPSLOTS.HEAD
    inst.components.equippable.insulated = true

    inst:AddComponent("insulator")
    inst.components.insulator:SetInsulation(0)

    inst:AddComponent("waterproofer")
    inst.components.waterproofer:SetEffectiveness(1)

    inst:AddComponent("container")
    inst.components.container:WidgetSetup("doro_orange_hat")
    inst.components.container:EnableInfiniteStackSize(true)

    inst:AddComponent("preserver")
    inst.components.preserver:SetPerishRateMultiplier(0)

    --受击1/3概率出现力场护盾
    inst.procfn = function(owner, data) tryproc(inst, owner, data) end
    inst.onattach = function(owner)
        if inst._owner ~= nil then
            inst:RemoveEventCallback("attacked", inst.procfn, inst._owner)
            inst:RemoveEventCallback("onremove", inst.ondetach, inst._owner)
        end
        inst:ListenForEvent("attacked", inst.procfn, owner)
        inst:ListenForEvent("onremove", inst.ondetach, owner)
        inst._owner = owner
        inst._fx = nil
    end
    inst.ondetach = function()
        ruinshat_unproc(inst)
        if inst._owner ~= nil then
            inst:RemoveEventCallback("attacked", inst.procfn, inst._owner)
            inst:RemoveEventCallback("onremove", inst.ondetach, inst._owner)
            inst._owner = nil
            inst._fx = nil
        end
    end
    inst.OnRemoveEntity = ruins_onremove
    return inst
end
local function fallofffn()
    return 1
end
local function doro_hat_aura()
    local inst = CreateEntity()
    inst.entity:AddTransform()
    inst:AddTag("NOBLOCK")
    if not TheWorld.ismastersim then
        return inst
    end
    --- 装备时产生半径4距离单位每分钟回san60的理智光环
    inst:AddComponent("sanityaura")
    inst.components.sanityaura.aura = 1
    inst.components.sanityaura.fallofffn = fallofffn
    inst.components.sanityaura.max_distsq = 4 * 4
    return inst
end

return Prefab("doro_orange_hat", fn, assets),
    Prefab("doro_hat_aura", doro_hat_aura, assets)
